//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.ScientificWorkflow.UIDesigner.Monitor
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Text;
    using System.ComponentModel;
    using System.Globalization;
    using System.Collections.Specialized;

    public class DataProductViewModel : INotifyPropertyChanged
    {
        private string activityName;
        private string activityLabel;
        private static int automationId;
       
        /// <summary>
        /// Gets and sets Activity Name
        /// </summary>
        public string ActivityName
        {
            get
            {
                return this.activityName;
            }
            set
            {
                if (this.activityName != value)
                {
                    this.activityName = value;
                    this.OnNotifyPropertyChanged("ActivityName");
                }
            }
        }

        /// <summary>
        /// Gets and sets Activity Label
        /// </summary>
        public string ActivityLabel
        {
            get
            {
                return this.activityLabel;
            }
            set
            {
                if (this.activityLabel != value)
                {
                    this.activityLabel = value;
                    this.OnNotifyPropertyChanged("ActivityLabel");
                }
            }
        }

        /// <summary>
        /// Static Property Intialise Automation Property Id 
        /// </summary>
        public static int AutomationIdSeed
        {
            get
            {
                return automationId;
            }
            set
            {
                automationId = value;
            }
        }

        /// <summary>
        /// Gets the Automation Id
        /// </summary>
        public static string DataProductAutomationId
        {
            get
            {
                return "DataProduct" + automationId++.ToString(CultureInfo.CurrentCulture);
            }
        }

        /// <summary>
        /// Gets and sets DataProductItem Collection
        /// </summary>
        public ObservableCollection<DataProductItem> DataProducts{ get; private set; }
        
        /// <summary>
        /// Gets and sets Iterations of the activity.
        /// </summary>
        public ObservableCollection<DataProductIterationViewModel> Iterations{ get; private set; }
        
        /// <summary>
        /// 
        /// </summary>
        public DataProductIterationViewModel CurrentIteration
        {
            get
            {
                if (this.Iterations.Count > 0)
                {
                    return this.Iterations[this.Iterations.Count - 1];
                }
                return null;
            }
        }

        /// <summary>
        /// The items displayed to the user. This contains both activities and iterations.
        /// </summary>
        public ObservableCollection<object> DisplayItems { get; private set; }
        

        /// <summary>
        /// Constructor
        /// </summary>
        public DataProductViewModel()
        {
            this.DisplayItems = new ObservableCollection<object>();
            this.DataProducts = new ObservableCollection<DataProductItem>();
            this.DataProducts.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChanged);
            this.Iterations = new ObservableCollection<DataProductIterationViewModel>();
            this.Iterations.CollectionChanged += new NotifyCollectionChangedEventHandler(CollectionChanged);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.NewItems.Count > 0)
            {
                if (e.NewItems[0] is DataProductItem)
                {
                    this.DisplayItems.Insert(e.NewStartingIndex, e.NewItems[0]);
                }
                else if (e.NewItems[0] is DataProductIterationViewModel)
                {
                    int offset = this.DataProducts.Count;
                    this.DisplayItems.Insert(e.NewStartingIndex + offset, e.NewItems[0]);
                }
            }
            else if(e.OldItems.Count > 0)
            {
                this.DisplayItems.Remove(e.OldItems[0]);
            }
        }

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnNotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
    }
}
